package com.bd.scm.module.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.offices.excel.ExcelUtil;
import com.bd.scm.dao.mapper.user.PersonnelFilesDao;
import com.bd.scm.domain.dto.user.PersonnelFilesDto;
import com.bd.scm.domain.models.user.PersonnelFiles;
import com.bd.scm.domain.models.user.PersonnelFilesImport;
import com.bd.scm.module.user.service.IPersonnelFilesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class PersonnelFilesServiceImpl implements IPersonnelFilesService {

    @Resource
    private PersonnelFilesDao personnelFilesDao;

    @Override
    public IPage<PersonnelFiles> getPersonList(PersonnelFilesDto personnelFilesDto) {
        Page<PersonnelFiles> page = new Page<PersonnelFiles>(personnelFilesDto.getCurrent(),personnelFilesDto.getPageSize());
        return personnelFilesDao.getPersonList(page,personnelFilesDto);
    }


    @Override
    public String quitPerson(List<PersonnelFilesDto>list, String clientIp) {
        String r = null;
        for (PersonnelFilesDto personnelFilesDto  : list) {
            PersonnelFilesDto j = personnelFilesDao.queryPersonById(personnelFilesDto);
            String user_lname = j.getUserLname();
            String user_no = j.getUserNo();
            int i = personnelFilesDao.quitPerson(personnelFilesDto);
            if(i>0){
                personnelFilesDto.setLogBz("姓名:"+user_lname+"  工号:"+user_no);
                personnelFilesDto.setLogIp(clientIp);
                personnelFilesDao.insertUserLogByQuit(personnelFilesDto);
                personnelFilesDao.insertWtPublicByQuit(personnelFilesDto);
            }else{
                r+=user_lname+"离职失败";
            }

        }
            return r;
    }


    @Override
    public String updatePerson(PersonnelFilesDto personnelFilesDto, String clientIp) {
        //返回信息字符串
        String r = null;
        Long user_serial = personnelFilesDao.getUserSerial(personnelFilesDto);
        if(user_serial!=null){
            personnelFilesDto.setUserSerial(user_serial);
            personnelFilesDto.setLogIp(clientIp);
            personnelFilesDao.updatePerson(personnelFilesDto);
            String acType = personnelFilesDao.getActypeById(personnelFilesDto);
            personnelFilesDto.setOldAcBh(acType);
            personnelFilesDao.updateAcType(personnelFilesDto);
            personnelFilesDao.execClientUserLimits(personnelFilesDto);
            personnelFilesDao.insertUserLogByUp(personnelFilesDto);
            personnelFilesDao.insertWtPublicByUp(personnelFilesDto);
        }else{
            r = "员工工号已经存在";
        }
        return r;
    }


    @Override
    @Transactional
    public String addPerson(PersonnelFilesDto personnelFilesDto, String clientIp) {
        //返回信息字符串
        String r = null;
        personnelFilesDto.setUserNo(String.valueOf(personnelFilesDto.getUserNo()));
        Long userSerial = personnelFilesDao.getUserSerial(personnelFilesDto);
        if(userSerial!=null){
            r = "员工工号已经存在";
        }else{
            PersonnelFilesDto serial = personnelFilesDao.getUserSerialAndNo();
            //TODO 获取最大ID后要把WT_MODULE表的ID加1
            Long user_serial = serial.getModuleUser();
            personnelFilesDto.setUserSerial(user_serial+1);

            System.out.println(personnelFilesDto);
            PersonnelFilesDto depInfo =  personnelFilesDao.getDepInfo(personnelFilesDto);

            personnelFilesDto.setDepNo(depInfo.getDepNo());
            personnelFilesDto.setDepName(depInfo.getDepName());
            personnelFilesDto.setLogIp(clientIp);
            personnelFilesDao.insertTmpDtUser(personnelFilesDto);
            /*String user_serial = personnelFilesDao.getUserSerial(jsonObject);
            jsonObject.put("user_serial", user_serial);*/
            personnelFilesDao.insertDtAcUser(personnelFilesDto);
            personnelFilesDao.insertDtAcLink(personnelFilesDto);
            String user_lname =personnelFilesDto.getUserLname();
            String user_no =personnelFilesDto.getUserNo();
            personnelFilesDto.setLogBz("姓名:"+user_lname+"  工号:"+user_no);
            personnelFilesDao.insertUserLogByAdd(personnelFilesDto);
            personnelFilesDao.insertKqfxRiZhi(personnelFilesDto);
            personnelFilesDao.insertWtPublicByAdd(personnelFilesDto);
        }
        return r;
    }


    @Override
    public String transferPerson(List<PersonnelFilesDto> list, String clientIp) {
        String r = null;
        for (PersonnelFilesDto personnelFilesDto : list) {
            personnelFilesDto.setLogIp(clientIp);
            PersonnelFilesDto depInfo= personnelFilesDao.getDepInfoNew(personnelFilesDto);
            personnelFilesDto.setDepNo(depInfo.getDepNo());
            personnelFilesDto.setDepName(depInfo.getDepName());
            PersonnelFilesDto j = personnelFilesDao.queryPersonById(personnelFilesDto);
            String user_lname = j.getUserLname();
            String user_no = j.getUserNo();
            String depSerial = personnelFilesDao.getDepSerial(personnelFilesDto);
            personnelFilesDto.setDepSerial(depSerial);
            personnelFilesDao.realUserRemove(personnelFilesDto);
            int i=personnelFilesDao.transferUser(personnelFilesDto);

            if(i>0){
                personnelFilesDto.setLogBz("姓名"+user_lname+"工号:"+user_no+"从"+personnelFilesDto.getDepName()+"调动到"+depInfo.getDepSerialNew());
                personnelFilesDao.insertTransferWt(personnelFilesDto);
                personnelFilesDao.insertTransferLog(personnelFilesDto);
            }else{

                r+=user_lname+"调动失败";
            }

        }
        return r;
    }


    @Override
    public List<PersonnelFilesDto> getacTypeList() {
        return personnelFilesDao.getacTypeList();
    }


    @Override
    public List<PersonnelFilesDto> getNation() {
        return personnelFilesDao.getNation();
    }


    @Override
    public List<PersonnelFilesDto> getNative() {
        return personnelFilesDao.getNative();
    }


    @Override
    public List<PersonnelFilesDto> getUserType() {
        return personnelFilesDao.getUserType();
    }


    @Override
    public PersonnelFilesDto getUserById(PersonnelFilesDto personnelFilesDto) {
        return personnelFilesDao.getUserById(personnelFilesDto);
    }


    @Override
    public PersonnelFilesDto getUserSerialAndNo() {
        personnelFilesDao.updateUserSerial();
        return personnelFilesDao.getUserSerialAndNo();
    }


    @Override
    public String uploadingExcel(MultipartFile file, HttpServletRequest request) throws IOException {
        String r = null;
        InputStream inputStream = file.getInputStream();
        List<PersonnelFilesImport> list = ExcelUtil.ImportExcel(inputStream, PersonnelFilesImport.class);
        try{
            for(PersonnelFilesImport personnelFilesImport : list){

                this.personnelFilesDao.addUser(personnelFilesImport);
                this.personnelFilesDao.insertTmpUser(personnelFilesImport);
                this.personnelFilesDao.updateTmpUser(personnelFilesImport);
                personnelFilesDao.exceSynchronization();
            }
        }catch (Exception e){
            r="导入数据有误!";
        }
        return r;
    }
}
